package com.workcell.workdata.xthings.jobhandler;

import cn.hutool.core.collection.ListUtil;
import com.workcell.workdata.xthings.service.PartitionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 分区表创建分区定时任务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PartitionJobHandler {
    private final PartitionService partitionService;

    /**
     * PostConstruct 注解的的方法会在 Bean 初始化之前执行一次（代替使用ApplicationRunner和CommandLineRunner）
     */
    @PostConstruct
    public void init() {
        log.info("PartitionJobHandler实例初始化时，执行判断创建分区操作。。。");
        createPartition();
    }

    @XxlJob("createPartition")
    public void createPartition() {
        List<String> tableList = ListUtil.of("t_device_run_log", "t_device_service_call_log");

        for (String table : tableList) {
            partitionService.createPartitionByDate(table);
        }

    }

}
